package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum CameraUploadErrorCode implements EnumTrans<CompleteEntity.CameraUploadErrorCode, CameraUploadErrorCode> {
    /**
     * CAMERA_DEFAULT
     * CAMERA_SUCCESS 配置相机成功
     * 相机异常
     * ERROR_UNKNOWN 配置摄像头未知异常
     * ERROR_URL_PATH URL地址错误
     * ERROR_UNAUTHORIZED 连接超时
     * ERROR_CONNECT_TIMEOUT 认证错误
     * ERROR_GET_USB_CAMERA 获取usb摄像头失败
     * 上次截图异常
     * CONNECT_SERVER_FAIL 无法访问服务器
     * SERVER_UNKNOWN_ERROR 上次截图未知异常
     */
    CAMERA_DEFAULT(CompleteEntity.CameraUploadErrorCode.CAMERA_DEFAULT),
    CAMERA_SUCCESS(CompleteEntity.CameraUploadErrorCode.CAMERA_SUCCESS),
    ERROR_UNKNOWN(CompleteEntity.CameraUploadErrorCode.ERROR_UNKNOWN),
    ERROR_URL_PATH(CompleteEntity.CameraUploadErrorCode.ERROR_URL_PATH),
    ERROR_UNAUTHORIZED(CompleteEntity.CameraUploadErrorCode.ERROR_UNAUTHORIZED),
    ERROR_CONNECT_TIMEOUT(CompleteEntity.CameraUploadErrorCode.ERROR_CONNECT_TIMEOUT),
    ERROR_GET_USB_CAMERA(CompleteEntity.CameraUploadErrorCode.ERROR_GET_USB_CAMERA),
    CONNECT_SERVER_FAIL(CompleteEntity.CameraUploadErrorCode.CONNECT_SERVER_FAIL),
    SERVER_UNKNOWN_ERROR(CompleteEntity.CameraUploadErrorCode.SERVER_UNKNOWN_ERROR),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.CameraUploadErrorCode cameraUploadErrorCode;

    CameraUploadErrorCode(CompleteEntity.CameraUploadErrorCode cameraUploadErrorCode) {
        this.id = cameraUploadErrorCode.getNumber();
        this.name = cameraUploadErrorCode.name();
        this.cameraUploadErrorCode = cameraUploadErrorCode;
    }

    /**
     * @param cameraUploadErrorCode
     * @return 对应的类型
     */
    public static CameraUploadErrorCode getCameraUploadErrorCode(CompleteEntity.CameraUploadErrorCode cameraUploadErrorCode) {
        Objects.requireNonNull(cameraUploadErrorCode, "cameraUploadErrorCode can not be null.");
        return Arrays.stream(CameraUploadErrorCode.values())
                .filter(e -> e.name.equals(cameraUploadErrorCode.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.CameraUploadErrorCode toProto() {
        return this.cameraUploadErrorCode;
    }

    @Override
    public CameraUploadErrorCode formProto(CompleteEntity.CameraUploadErrorCode cameraUploadErrorCode) {
        return getCameraUploadErrorCode(cameraUploadErrorCode);
    }
}

